﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace LAB.Manager
{
    public static class SubscribeManager
    {
        //t_subscribe
        //订阅器
        //id
        //interval		发送周期
        //finderId		搜索器Id

        static SubscribeManager()
        {
            // 

        }


        public static bool insert(int finderId, int interval, string owner)
        {
            var rst = false;
           
            using (var db = new TagerEntities())
            {
                var finder = db.t_finder.FirstOrDefault(c => c.id == finderId);
                if (finder != null && finder.owner == owner)
                {
                    var subs = new t_subscribe { finderId = finderId, interval = interval };
                    db.t_subscribe.Add(subs);
                    db.SaveChanges();
                    rst = true;
                }
            }
            return rst;
        }


        public static bool edit(int finderId, int interval, string owner)
        {
            var rst = false;
          
            using (var db = new TagerEntities())
            {
                var subs = db.t_subscribe.FirstOrDefault(c => c.finderId == finderId);
                if (subs != null)
                {
                    subs.interval = interval;
                    db.SaveChanges();

                
                }
            }
            return rst;
        }


        public static bool delete(int finderId, string owner)
        {
            var rst = false;
            using (var db = new TagerEntities())
            {
                var subs = db.t_subscribe.FirstOrDefault(c => c.id == finderId);
                if (subs != null)
                {
                    var finder = db.t_finder.FirstOrDefault(c => c.id == subs.finderId);
                    if (finder.owner == owner || SysUtil.isSysEditor(owner))
                    {
                        db.t_subscribe.Remove(subs);
                        db.SaveChanges();
                        rst = true;
                    }
                }
            }
            return rst;
        }




    }
}
